package com.token.iot.bratterycharging.boss.web;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.container.springmvc.WebContext;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ReturnCode;
import com.token.base.order.api.PayItemDTO;
import com.token.iot.bratterycharging.boss.service.MerchantService;
import com.token.iot.bratterycharging.boss.service.OrderServcice;

@Controller
@RequestMapping("/merchant")
public class MerchantController {

	
	@Autowired
	private MerchantService merchantService;

	@ResponseBody
	@RequestMapping(value = "/login.do")
	public Merchant login(@RequestBody Merchant dto) throws PendingException {
		TransactionLogger.debug("staring login " + String.valueOf(dto == null) + " ");
		// dto.setRole("管理员");
		Merchant merchant = merchantService.selectByUserNamePassword(dto.getCode(), dto.getPassword());
		if (merchant == null) {
			throw new PendingException(ResCode.LoginError);
		}
		WebContext.getSession().setAttribute("_merchantid", String.valueOf(merchant.getId()));
		WebContext.getSession().setAttribute("_merchant", merchant);
		return merchant;
	}

	/**
	 * 添加商户
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/add.do")
	public String add(@RequestBody Merchant dto) throws PendingException {
		TransactionLogger.debug("staring Merchant add " + String.valueOf(dto == null) + " ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				throw new PendingException(ResCode.OperNotLogin);
			}
			dto.setAgentId(agentid);
		}
		// 如果operid 不为空，则需要传入 agentid

		return merchantService.addMerchant(dto, operid);
	}

	/**
	 * 商户详情
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/detail.do")
	public Merchant detail(String merchantId) throws PendingException {
		TransactionLogger.debug("staring merchant detail  ");
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		String merid = merchantId;
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				String mid = (String) WebContext.getSession().getAttribute("_merchantid");
				if (mid == null) {
					throw new PendingException(ResCode.OperNotLogin);
				} else {
					merid = mid;
				}
			}
		}
		// 如果operid 不为空，则需要传入 agentid

		return merchantService.detail(merid);
	}

	/**
	 * 列出所属商户
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/list.do")
	public PageInfo list(@RequestBody ReqPageDto dto) throws PendingException {
		
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		String aid = dto.getAgentId();
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				throw new PendingException(ResCode.OperNotLogin);
				
			} else {
				aid = agentid;
			}
		}
		
		if (StringUtils.isBlank(dto.getMerchantName())) {
			// 如果operid 不为空，则需要传入 agentid
			return merchantService.list(dto.getPageNo(), dto.getPageSize(), aid);
		} else {
			// 如果operid 不为空，则需要传入 agentid
			return merchantService.list(dto.getPageNo(), dto.getPageSize(), aid, dto.getMerchantName());
		}
	}

	/**
	 * 列出所属商户
	 * 
	 * @param dto
	 * @return
	 * @throws PendingException
	 */
	@ResponseBody
	@RequestMapping(value = "/listall.do")
	public List<Merchant> listall(String agentId) throws PendingException {
		Long operid = (Long) WebContext.getSession().getAttribute("_operid");
		String merid = agentId;
		if (operid == null) {
			String agentid = (String) WebContext.getSession().getAttribute("_agentid");
			if (agentid == null) {
				throw new PendingException(ResCode.OperNotLogin);
			}else {
				merid = agentid;
			}
		}
		// 如果operid 不为空，则需要传入 agentid
		return merchantService.listall(merid);

	}

}
